package com.runing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.runing.config.ResponseWrapper;
import com.runing.entity.RSemester;
import com.runing.entity.RWeek;
import com.runing.mapper.RWeekMapper;
import com.runing.service.RWeekService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-11
 */
@Service
public class RWeekServiceImpl extends ServiceImpl<RWeekMapper, RWeek> implements RWeekService {
    @Autowired
    RWeekMapper rWeekMapper;
    @Autowired
    RSemesterServiceImpl rSemesterService;

    //    cron 表达式
//    秒 分 时 日 月 周几
//    30 15 10 * * ？每天十点15分执行一次
//      30 0/5 10，18 * * ？ 每天10和18点，没隔五分钟执行一次
//    0 15 10 ？ * 1-6 每个月的周一到周六 十点十五分执行一次
    //每周一执行一次
//    0 0 0 ? * 1
    @Scheduled(cron = "0 0 0 ? * 1")
    public void addweek(){
//        Calendar cal = Calendar.getInstance();
//        int month = (cal.get(Calendar.MONTH)) + 1;
//        int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
//        if ((month>=3 && day_of_month >0)&&(month<8 && day_of_month <20)){
            RSemester rSemester = rSemesterService.findNowSemester();
            QueryWrapper<RWeek> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("seid",rSemester.getSeid()).orderByDesc("wid").last("limit 1");
            RWeek week = rWeekMapper.selectOne(queryWrapper);
            RWeek rWeek =new RWeek();
            rWeek.setSeid(week.getSeid());
            rWeek.setWname(String.valueOf(Integer.valueOf(week.getWname())+1));
            rWeekMapper.insert(rWeek);
            System.out.println("hello");
//        }
    }

    public ResponseWrapper nowSemesterAndWeek(){
        RSemester rSemester = rSemesterService.findNowSemester();
        QueryWrapper<RWeek> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seid",rSemester.getSeid()).orderByDesc("wid").last("limit 1");
        RWeek week = rWeekMapper.selectOne(queryWrapper);
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(week);
        wrapper.getData().put("rSemester",rSemester);
        wrapper.getData().put("week",week);
        return wrapper;
    }
    public ArrayList allWeeks(){
        ArrayList<Object> semesterBeans = new ArrayList<>();
        List<RSemester> semesterList = rSemesterService.findAllRSemester();
        for (RSemester rSemester : semesterList){
            HashMap<Object, Object> weeks = new HashMap<>();
            ArrayList<Object> weekBeanList = new ArrayList<>();
            int seid  =rSemester.getSeid();
            QueryWrapper<RWeek> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("seid",seid);
            List<RWeek> weekList = rWeekMapper.selectList(queryWrapper);
            for(RWeek rWeek :weekList){
                weekBeanList.add(rWeek);
            }
            weeks.put("seid",seid);
            weeks.put("sename",rSemester.getSename());
            weeks.put("weekBeanList",weekBeanList);
            semesterBeans.add(weeks);

        }
        return semesterBeans;
    }

}
